using System.Windows;
using System.Windows.Controls;
using ALC.Core.ViewModels.GraphicPack;
using HalconDotNet;

namespace ALC.Views.GraphicPack
{
    public partial class GraphicPackView : UserControl
    {
        private HWindow _windowHandle;
        private bool _hasLoaded = false;
        public GraphicPackView()
        {
            InitializeComponent();
            Loaded += OnGraphicPackViewLoaded;
        }



        private void OnGraphicPackViewLoaded(object sender, RoutedEventArgs e)
        {
            if (_hasLoaded) return;
            _windowHandle = PART_HalconWindow.HalconWindow;
            _windowHandle?.SetColored(3);
            _windowHandle?.SetPart(0, 0, -2, -2);
            _windowHandle?.SetColor("green");
            _windowHandle?.SetDraw("margin");
            _hasLoaded = true;
        }

        public static readonly DependencyProperty GraphicPackProperty = DependencyProperty.Register(
            "GraphicPack", typeof(GraphicPackViewModel), typeof(GraphicPackView),
            new PropertyMetadata(default(GraphicPackViewModel), OnGraphicPackChanged));

        private static void OnGraphicPackChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var view = (GraphicPackView) d;
            var newValue = (GraphicPackViewModel) e.NewValue;

            if (newValue == null) return;
            view._windowHandle?.DispImage(newValue.Image);
            if (newValue.Graphics != null) view._windowHandle?.DispObj(newValue.Graphics);
        }

        public GraphicPackViewModel GraphicPack
        {
            get { return (GraphicPackViewModel) GetValue(GraphicPackProperty); }
            set { SetValue(GraphicPackProperty, value); }
        }
    }
}